package com.yingxin.yxerp.module.inform.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yingxin.yx.framework.commons.enums.BusinessType;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yxerp.module.inform.entity.Notice;
import com.yingxin.yxerp.module.inform.mapper.NoticeMapper;
import com.yingxin.yxerp.module.inform.service.INoticeService;
import com.yingxin.yxerp.module.inform.vo.req.NoticeQueryVo;
import com.yingxin.yxerp.module.inform.vo.resp.NoticeRespVo;
import com.yingxin.yxerp.module.oplog.aop.OplogAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 通知公告-公告 服务实现类
 * @author zhuchangjie
 * @since 2021-12-21
 */

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements INoticeService {

    @Autowired
    private IUserSupport userSupport;

    /**
     * 分页查询
     */
    @Override
    public IPage<NoticeRespVo> pageVo(Page page, NoticeQueryVo queryVo) {
        IPage<NoticeRespVo> voPage = baseMapper.pageVo(page, queryVo);
        return voPage;
    }

    /**
     * 查询全部
     */
    @Override
    public List<NoticeRespVo> listVo(NoticeQueryVo queryVo) {
        List<NoticeRespVo> voList = baseMapper.listVo(queryVo);
        return voList;
    }

    /**
     * app 查询分页
     */
    @Override
    public IPage<NoticeRespVo> pageAppVo(Page page, NoticeQueryVo queryVo){
        queryVo.setUserId( userSupport.getUserId() );
        IPage<NoticeRespVo> voPage = baseMapper.pageAppVo( page, queryVo );
        return voPage;
    }

    /**
     * app 查询
     */
    @Override
    public List<NoticeRespVo> listAppVo(NoticeQueryVo queryVo) {
        queryVo.setUserId( userSupport.getUserId() );
        return baseMapper.listAppVo( queryVo );
    }

    /**
     * 根据id查询
     */
    @Override
    public NoticeRespVo getVo(String id) {
        NoticeRespVo vo = baseMapper.getVo(id);
        return vo;
    }

    /**
     * 创建
     */
    @OplogAnnotation(biz = BusinessType.TZGG, id = "{{notice.id}}", action = "新增", desc = "标题：{{notice.title}}")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void create(Notice notice) {
        save(notice);
    }

    /**
     * 修改
     */
    @OplogAnnotation(biz = BusinessType.TZGG, id = "{{notice.id}}", action = "修改", desc = "标题：{{notice.title}}")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void update(Notice notice) {
        updateById(notice);
    }

    /**
     * 根据id删除
     */
    @OplogAnnotation(biz = BusinessType.TZGG, id = "{{notice.id}}", action = "删除", desc = "标题：{{notice.title}}")
    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void delete(Notice notice) {
        removeById(notice);
    }

}
